package com.tongfusoft.terminal.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.tongfusoft.base.ExtConstant;
import com.tongfusoft.terminal.utils.ParamUtils;
import com.tongfusoft.web.core.Constant;
import com.tongfusoft.web.core.data.DictionaryData;
import com.tongfusoft.web.core.data.OptionData;
import com.tongfusoft.web.core.service.IOptionListService;

public class OptionListServiceImpl implements IOptionListService {

	@Override
	public Map<String, List<OptionData>> getOptions(Map params) {
		// 获取查询的key，固定值：dictCode
		String dictCodes = (String) params.get(Constant.REQDICTCODE);

		String[] dictCode = StringUtils.split(dictCodes, ",");

		String currProject = "1";

		Map<String, List<OptionData>> result = new HashMap<String, List<OptionData>>();

		for (int i = 0; dictCode != null && i < dictCode.length; i++) {
			List<DictionaryData> dicts = ParamUtils.findDictionaryByCode(
					(Map<String, Map<String, List<DictionaryData>>>) params.get(ExtConstant.DICTIONARY), dictCode[i],
					currProject);
			if (dicts == null) {
				dicts = Collections.EMPTY_LIST;
			}
			result.put(dictCode[i], getOptions(dicts));
		}

		return result;
	}

	private List<OptionData> getOptions(List<DictionaryData> dicts) {
		List<OptionData> list = new ArrayList<OptionData>();
		for (DictionaryData dict : dicts) {
			list.add(dict.getOptionData());
		}
		return list;
	}

}
